package com.zheng.pay.admin.manage.controller;

import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.ResultCollectors;
import com.zheng.common.base.BaseController;
import com.zheng.common.constant.Constant;
import com.zheng.common.util.DateUtil;
import com.zheng.common.validator.LengthValidator;
import com.zheng.pay.common.constant.PayResult;
import com.zheng.pay.common.constant.PayResultConstant;
import com.zheng.pay.dao.model.PayMchInfo;
import com.zheng.pay.dao.model.PayMchInfoExample;
import com.zheng.pay.dao.model.PayPayChannel;
import com.zheng.pay.dao.model.PayPayChannelExample;
import com.zheng.pay.rpc.api.PayMchInfoService;
import com.zheng.pay.rpc.api.PayPayChannelService;
import com.zheng.upms.dao.model.UpmsDict;
import com.zheng.upms.rpc.api.UpmsApiService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Api(value = "支付渠道管理", description = "支付渠道管理")
@RequestMapping("/manage/channel")
public class PayChannelController  extends BaseController {

    private static final Logger _log = LoggerFactory.getLogger(PayChannelController.class);

    @Autowired
    private PayPayChannelService payPayChannelService;
    @Autowired
    private UpmsApiService upmsApiService;
    @Autowired
    private PayMchInfoService payMchInfoService;


    @ApiOperation(value = "支付渠道首页")
    @RequiresPermissions("pay:mchchannel:read")
    @RequestMapping(value = "/index", method = {RequestMethod.GET, RequestMethod.POST})
    public String index(ModelMap modelMap) {
        //支付渠道类型
        List<UpmsDict> channelList = upmsApiService.selectUpmsDictByCode(Constant.PAY_CHANNEL);
        modelMap.put("channelList", channelList);
        //商户列表
        PayMchInfoExample machInfoExample = new PayMchInfoExample();
        machInfoExample.createCriteria().andStateEqualTo((byte)Constant.CONST_ENABLE);
        List<PayMchInfo> payMchList = payMchInfoService.selectByExample(machInfoExample);
        modelMap.put("payMchList", payMchList);
        return "/manage/channel/index.jsp";
    }

    @ApiOperation(value = "支付渠道列表")
    @RequiresPermissions("pay:mchchannel:read")
    @RequestMapping(value = "/list", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Object list(
            @RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            @RequestParam(required = false, value = "sort") String sort,
            @RequestParam(required = false, value = "order") String order) {

        PayPayChannelExample payPayChannelExample= new PayPayChannelExample();
        if (!StringUtils.isBlank(sort) && !StringUtils.isBlank(order)) {
            payPayChannelExample.setOrderByClause(sort + " " + order);
        }
        List<PayPayChannel> rows = payPayChannelService.selectByExampleForOffsetPage(payPayChannelExample, offset, limit);
        long total = payPayChannelService.countByExample(payPayChannelExample);
        Map<String, Object> result = new HashMap<>(2);
        result.put("rows", rows);
        result.put("total", total);
        return result;
    }

    @ApiOperation(value = "新增")
    @RequiresPermissions("pay:mchchannel:create")
    @RequestMapping(value = "/create", method = {RequestMethod.GET})
    public String create(ModelMap modelMap) {
        //支付渠道类型
        List<UpmsDict> channelList = upmsApiService.selectUpmsDictByCode(Constant.PAY_CHANNEL);
        modelMap.put("channelList", channelList);
        //商户列表
        PayMchInfoExample machInfoExample = new PayMchInfoExample();
        machInfoExample.createCriteria().andStateEqualTo((byte)Constant.CONST_ENABLE);
        List<PayMchInfo> payMchList = payMchInfoService.selectByExample(machInfoExample);
        modelMap.put("payMchList", payMchList);
        return "/manage/channel/create.jsp";
    }

    @ApiOperation(value = "新增支付渠道")
    @RequiresPermissions("pay:mchchannel:create")
    @RequestMapping(value = "/create", method = {RequestMethod.POST})
    @ResponseBody
    public Object create(PayPayChannel payPayChannel) {
        Date time = DateUtil.getNowDate();
        payPayChannel.setUpdatetime(time);
        payPayChannel.setCreatetime(time);
        int count = payPayChannelService.insertSelective(payPayChannel);
        return new PayResult(PayResultConstant.SUCCESS, count);
    }

    @ApiOperation(value = "删除支付渠道")
    @RequiresPermissions("pay:mchchannel:delete")
    @RequestMapping(value = "/delete/{ids}",method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Object delete(@PathVariable("ids") String ids) {
        int count = payPayChannelService.deleteByPrimaryKeys(ids);
        return new PayResult(PayResultConstant.SUCCESS, count);
    }

    @ApiOperation(value = "修改支付渠道")
    @RequiresPermissions("pay:mchchannel:update")
    @RequestMapping(value = "/update/{id}", method = {RequestMethod.GET})
    public String update(@PathVariable("id") int id, ModelMap modelMap) {
        PayPayChannelExample payPayChannelExample = new PayPayChannelExample();
        payPayChannelExample.setOrderByClause("CreateTime desc");
        PayPayChannel payPayChannel = payPayChannelService.selectByPrimaryKey(id);
        modelMap.put("payPayChannel", payPayChannel);
        //支付渠道类型
        List<UpmsDict> channelList = upmsApiService.selectUpmsDictByCode(Constant.PAY_CHANNEL);
        modelMap.put("channelList", channelList);
        //商户列表
        PayMchInfoExample machInfoExample = new PayMchInfoExample();
        machInfoExample.createCriteria().andStateEqualTo((byte)Constant.CONST_ENABLE);
        List<PayMchInfo> payMchList = payMchInfoService.selectByExample(machInfoExample);
        modelMap.put("payMchList", payMchList);
        return "/manage/channel/update.jsp";
    }

    @ApiOperation(value = "修改支付渠道")
    @RequiresPermissions("pay:mchchannel:update")
    @RequestMapping(value = "/update/{id}", method = {RequestMethod.POST})
    @ResponseBody
    public Object update(@PathVariable("id") int id, PayPayChannel payPayChannel) {
        payPayChannel.setId(id);
        int count = payPayChannelService.updateByPrimaryKeySelective(payPayChannel);
        return new PayResult(PayResultConstant.SUCCESS, count);
    }


}